package com.longjianghuasheng.logistics.business.register

import android.os.Bundle
import com.longjianghuasheng.logistics.BR
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.data.bindadapter.CustomBindAdapter.afterTextChanged
import com.longjianghuasheng.logistics.databinding.FragmentRegisterBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.changeBtn
import com.longjianghuasheng.logistics.ext.checkPhone
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.util.CacheUtil
import com.longjianghuasheng.logistics.util.StatusBarUtil
import me.hgj.jetpackmvvm.ext.nav

class RegisterFragment : BaseFragment<RegisterViewModel, FragmentRegisterBinding>() {

    override fun initViewModelId(): Int = BR.viewModelRegister


    override fun layoutId(): Int = R.layout.fragment_register

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.run {
            edtPhone.afterTextChanged {
                btnRegister.changeBtn {
                    checkPhone(it, false) && edtCode.text.toString()
                        .isNotBlank() && !edtPassword.text.isNullOrBlank()
                }
            }
            edtCode.afterTextChanged {
                btnRegister.changeBtn {
                    checkPhone(
                        edtPhone.text.toString(),
                        false
                    ) && it.isNotBlank() && !edtPassword.text.isNullOrBlank()
                }
            }

            edtPassword.afterTextChanged {
                btnRegister.changeBtn {
                    checkPhone(
                        edtPhone.text.toString(),
                        false
                    ) && it.isNotBlank() && !edtCode.text.isNullOrBlank()
                }
            }


        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            CacheUtil.isLogin = true
            CacheUtil.token = it.token ?: ""
            CacheUtil.isDriver = it.type == 2
            eventViewModel.loginEvent.value = 0
            nav().popBackStack(R.id.main2Fragment, false)
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun onResume() {
        super.onResume()
        StatusBarUtil.setLightMode(requireActivity())
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

}